﻿using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class list_content_mail : System.Web.UI.Page
    {

        static SendContentBUS scBus = null;
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();

            scBus = new SendContentBUS();
            if (!IsPostBack)
            {
                try
                {
                    //LoadDataListContent();
                }
                catch (Exception ex)
                {
                    pnError.Visible = true;
                    lblError.Text = ex.Message;
                }

            }
        }


        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            return null;
        }

        protected void visibleMessage(bool vis)
        {
            pnError.Visible = vis;
            pnSuccess.Visible = vis;
        }
        protected void btnPreview_Click(object sender, ImageClickEventArgs e)
        {
            int ContentId = int.Parse(((ImageButton)sender).CommandArgument);
            if (scBus.GetByID(ContentId).Rows.Count > 0)
            {
                Session["Content"] = scBus.GetByID(ContentId).Rows[0]["Body"].ToString();
            }
            else
            {
                Session["Content"] = "Không có nội dung";
            }
            Response.Redirect("PreviewContent.aspx");
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<SendContentDTO> Search()
        {

            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            DataTable lsContent = new DataTable();
            if (userLogin.DepartmentId == 1)
            {
                lsContent = scBus.GetAll();
            }
            else
            {

                lsContent = scBus.GetAllSendContent(userLogin.UserId, Common.contentType.All);
            }
            List<SendContentDTO> result = new List<SendContentDTO>();
            SendContentDTO item = new SendContentDTO();
            foreach (DataRow dr in lsContent.Rows)
            {
                item = new SendContentDTO();
                item.Id = Convert.ToInt32(dr["Id"] + "");
                item.Subject = dr["Subject"] + "";
                item.ContentType = Convert.ToInt32(dr["ContentType"] + "");
                result.Add(item);
            }

            return result;
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string Delete(int id)
        {
            try
            {
                //SignatureBUS signBus = new SignatureBUS();
                ConnectionData.OpenMyConnection();
                scBus.tblSendContent_Delete(id);
                ConnectionData.CloseMyConnection();
            }
            catch (Exception ex)
            {
                common.Common.logs(userLogin.Username+"-ListContent-Delete",ex, Common.logType.error);
            }
            return "Xóa thành công!";
        }
    }

}